import random

from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from utils.jwttool import tooljwt
from utils.myredis import redis_toll
from .models import UserModel, AddressModel
from .tasks import send
from .userser import AddressSerializer, UserSerializer


# Create your views here.

class SendCode(APIView):
    def post(self, request):
        phone = request.data.get('phone')
        code = str(random.randint(1000, 9999))
        redis_code = redis_toll.get_str(phone)
        if redis_code:
            return Response({"code": 400, "message": "请勿重复发送", })
        else:
            send.delay(phone, code)
            redis_toll.set_str(phone, code, 60 * 2)
            return Response({"code": 200, "message": "发送成功", })


class LoginView(APIView):
    def post(self, request):
        phone = request.data.get('phone')
        code = request.data.get('code')
        redis_code = redis_toll.get_str(phone)
        if redis_code != code:
            return Response({"code": 400, "message": "验证码错误", })
        user = UserModel.objects.filter(phone=phone).first()
        if user is None:
            return Response({"code": 400, "message": "用户不存在", })
        token = tooljwt.getToken(user.id, user.phone)
        id = user.id
        return Response({"code": 200, "message": "登录成功", 'data': {'id': id, 'token': token, 'phone': phone}})


class RegisterView(APIView):
    def post(self, request):
        phone = request.data.get('phone')
        code = request.data.get('code')
        redis_code = redis_toll.get_str(phone)
        if redis_code != code:
            return Response({"code": 400, "message": "验证码错误", })
        user = UserModel.objects.filter(phone=phone).first()
        if user is not None:
            return Response({"code": 400, "message": "用户已存在", })
        user = UserModel.objects.create(phone=phone)
        token = tooljwt.getToken(user.id, user.phone)
        id = user.id
        return Response({"code": 200, "message": "注册成功", 'data': {'id': id, 'token': token, 'phone': phone}})


class AddressView(APIView):
    def get(self, request):
        user = request.query_params.get('user')
        address = AddressModel.objects.filter(user=user)
        ser = AddressSerializer(address, many=True)
        return Response(ser.data)

class GetAddress(APIView):
    def get(self, request):
        id = request.query_params.get('id')
        address = AddressModel.objects.filter(id=id).first()
        ser = AddressSerializer(address)
        return Response(ser.data)


class NewAddressView(APIView):
    def post(self, request):
        user = request.data.get('user')
        a_name = request.data.get('a_name')
        a_phone = request.data.get('a_phone')
        address = request.data.get('address')
        add = AddressModel.objects.filter(user=user, a_name=a_name, a_phone=a_phone, address=address).first()
        if add:
            return Response({"code": 400, "message": "地址已存在,请勿重复添加", })
        ser = AddressSerializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200, "message": "添加成功", 'data': ser.data})
        else:
            return Response({"code": 400, "message": "添加失败", })


class UpdAddressView(APIView):
    def put(self, request):
        id = request.data.get('id')
        a_name = request.data.get('a_name')
        a_phone = request.data.get('a_phone')
        address = request.data.get('address')
        add = AddressModel.objects.filter(a_name=a_name, a_phone=a_phone, address=address).first()
        if add:
            return Response({"code": 400, "message": "地址已存在,无法修改", })
        data = AddressModel.objects.filter(id=id).first()
        data.a_name = a_name
        data.a_phone = a_phone
        data.address = address
        data.save()
        ser = AddressSerializer(data)
        return Response({"code": 200, "message": "修改成功", 'data': ser.data})


class DelAddressView(APIView):
    def delete(self, request):
        id = request.query_params.get('id')
        AddressModel.objects.filter(id=id).delete()
        return Response({"code": 200, "message": "删除成功", })


class ChangeDef(APIView):
    def put(self, request):
        id = request.data.get('id')
        user = request.data.get('user')
        all_data = AddressModel.objects.filter(user=user)
        for i in all_data:
            i.is_def = False
            i.save()
        data = AddressModel.objects.filter(id=id).first()
        data.is_def = True
        data.save()
        ser = AddressSerializer(data)
        return Response({"code": 200, "message": "修改成功", 'data': ser.data})


class ChangeSelect(APIView):
    def put(self, request):
        id = request.data.get('id')
        user = request.data.get('user')
        all_data = AddressModel.objects.filter(user=user)
        for i in all_data:
            i.select = False
            i.save()
        data = AddressModel.objects.filter(id=id).first()
        data.select = True
        data.save()
        ser = AddressSerializer(data)
        return Response({"code": 200, "message": "修改成功", 'data': ser.data})


class GetSelectId(APIView):
    def get(self, request):
        user = request.query_params.get('user')
        data = AddressModel.objects.filter(user=user, select=True).first()
        return Response({"code": 200, "message": "获取成功", 'id': data.id})


class GetUserView(APIView):
    def get(self, request):
        user = request.query_params.get('user')
        data = UserModel.objects.filter(id=user).first()
        ser = UserSerializer(data)
        return Response({"code": 200, "message": "获取成功", 'data': ser.data})


class UpdUser(APIView):
    def put(self, request):
        user = request.query_params.get('user')
        data = UserModel.objects.filter(id=user).first()
        ser = UserSerializer(data, data=request.data, partial=True)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200, "message": "保存成功", 'data': ser.data})
        else:
            return Response({"code": 400, "message": "保存失败", 'err': ser.errors})
